#include "../mysql-connector-c-6.1.11/include/mysql.h"
#include <iostream>
#include <string>

const std::string host = "127.0.0.1";
// const std::string host="localhost";
const std::string user = "connector";
const std::string passwd = "041116Dc!";
const std::string db = "conn";
unsigned int port = 3306;
int main()
{
    // std::cout<<"mysql client version: "<<mysql_get_client_info()<<std::endl;
    MYSQL *my = mysql_init(nullptr);
    if (my == nullptr)
    {
        perror("init error\n");
        return 1;
    }
    if (mysql_real_connect(my, host.c_str(), user.c_str(), passwd.c_str(), db.c_str(), port, nullptr, 0) == nullptr)
    {
        perror("connector error\n");
        return 2;
    }
    mysql_set_character_set(my,"utf8mb4");
    // std::cout<<"connect success\n"<<std::endl;
    // mysql_query(my,"insert into user values(1,'dc');");
    std::string sql="select * from user;";
    if (mysql_query(my,sql.c_str()))
    {
        perror("query error\n");
        return 3;
    }
    MYSQL_RES *res=mysql_store_result(my);
    if (res==nullptr)
    {
        perror("store result error\n");
        return 4;
    }
    MYSQL_FIELD *fields=mysql_fetch_fields(res);
    for (int i=0;i<mysql_num_fields(res);i++)
    {
        std::cout<<fields[i].name<<"\t";
    }
    std::cout<<std::endl;
    while (MYSQL_ROW row=mysql_fetch_row(res))
    {
        for (int i=0;i<mysql_num_fields(res);i++)
        {
            std::cout<<row[i]<<"\t";
        }
        std::cout<<std::endl;
    }

    mysql_close(my);
    return 0;
}